﻿//显示tips
(function ($) {
    var step = 800; //a
    $.fn.showTip = function () {
        var Message = this.attr("tip");
        if (this.get(0).tipContainer) {
            $(".content", this.get(0).tipContainer).html(Message);
            $(this.get(0).tipContainer).fadeIn(step);
            return;
        }
        var tip = document.createElement("div")
        this.get(0).tipContainer = tip;
        tip.className = "jquery_tip";

        var divHead = document.createElement("div");
        var divContent = document.createElement("div");
        var divFoot = document.createElement("div");
        divHead.className = "head";
        divContent.className = "content";
        divFoot.className = "foot";
        divContent.innerHTML = Message;
        tip.appendChild(divContent);

        var pos = {
            left: this.offset().left - 2,
            top: this.offset().top + this.height() + 14
        }
        var mywidth = (this.width()) > 100 ? (this.width()) : 100;
        if ($(this).attr("tagName").toLowerCase() == "div") {
            pos = {
                left: this.offset().left+1,
                top: this.offset().top + this.height()
            }
            mywidth=(this.width()) > 100 ? (this.width()-15) : 100;
        }
        
        $(tip).css({ top: pos.top, left: pos.left, width: mywidth });

        document.body.appendChild(tip);
        $(tip).fadeIn(step);
    }
    $.fn.hideTip = function () {
        if (this.get(0).tipContainer) { $(this.get(0).tipContainer).fadeOut(100); }
    }
})(jQuery);

//验证表单
(function ($) {
    $.checkForm = function () {
        var CheckWithTip = function () {
            if (Check.apply(this))
                $(this).hideTip();
            else
                $(this).showTip();
        }

        //检测无提示
        var CheckNoTip = function () {
            $(this).hideTip();
            return Check.apply(this);
        }

        //检测方法
        function Check() {
            if ($(this).attr("lang").indexOf("SelArea") != -1) {
                if ($(this).parent().attr("EnableCheck") != "True")
                    return true;
            }
            else {
                if ($(this).attr("EnableCheck") != "True")
                    return true;
            }
            
            //验证不能为空 默认不能为空
            if ($(this).attr("EnableNull") != "True") {
                if ($.trim(this.value).length == 0 && $(this).attr("tagName").toLowerCase() != "select" && $(this).attr("tagName").toLowerCase() != "table" && $(this).attr("tagName").toLowerCase() != "div") {
                    $(this).addClass("checkError");
                    this.tip = "必填";
                    return false;
                }
                else if ($(this).attr("tagName").toLowerCase() == "select") {
                    if ($(this).attr("lang").indexOf("SelArea") != -1) {
                        if (this.value == "null") {
                            $(this).addClass("checkError");
                            this.tip = "请选择项！";
                            return false;
                        }
                    }
                    else {
                        if (this.value == $(this).attr("UnEnabledValue")) {
                            $(this).addClass("checkError");
                            this.tip = "请选择项！";
                            return false;
                        }
                    }
                }
                else if ($(this).attr("tagName").toLowerCase() == "table") {
                    var myflag = true;
                    if ($(":checkbox:checked", this).size() == 0) {
                        myflag = false;
                        $(this).addClass("checkError");
                    }
                    if (!myflag) {
                        return false;
                    }
                }
                else if ($(this).attr("tagName").toLowerCase() == "div") {
                    var myflag = true;
                    if ($(":hidden", this).val()==null || $(":hidden", this).val() == '' || $(":hidden", this).val().substr(4, 2) == "00") {
                        myflag = false;
                        this.tip = "请选择项！";
                        $("select", this).addClass("checkError");
                        $("select:eq(0)", this).removeClass("checkError");
                    }
                    else {
                        $("select", this).removeClass("checkError");
                    }
                    if (!myflag) {
                        return false;
                    }
                }
            }
            else if ($.trim(this.value).length == 0) {
                $(this).removeClass("checkError");
                return true;
            }
            if (CheckType.apply(this, [$(this).attr("CheckType")]).test(this.value)) {
                $(this).removeClass("checkError");
                return true;
            }
            else {
                $(this).addClass("checkError");
                this.tip = CastErrorMessage.apply(this, [$(this).attr("CheckType")]);
                return false
            }
        }

        //检测类型
        function CheckType(type) {
            switch (type) {
                case "Mail":        //电子信箱
                    return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
                case "Int":         //整数
                    return /^\d+$/;
                case "Decimal":     //浮点数
                    return /^\d+(\.\d+)?$/;
                case "DateTime":    //日期
                    return /^(\d{4})-(\d{2})-(\d{2})$/;
                case "PhoneNumber": //电话号码
                    return /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;
                case "Phone": //手机号码
                    return /^\d{11}$/;
                case "ZipCode":    //邮政编码
                    return /^\d{6}$/;
                case "Money":    //货币（正数或负数）
                    return /^[+-]?\d*\.?\d{1,3}$/;
                case "IDCard":
                    return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                default:
                    if (this.CustomRegular)
                        return eval("/" + this.CustomRegular + "/");
                    else
                        return /.*/
            }
        }

        //错误提示信息
        function CastErrorMessage(type) {
            switch (type) {
                case "Mail":        //电子信箱
                    return "邮箱格式错误<br/>示例：zhmarkets@163.com";
                case "Int":         //整数
                    return "请填写整数";
                case "Decimal":     //浮点数
                    return "请填写整数或小数";
                case "DateTime":    //日期
                    return "日期格式错误<br/>示例：2015-01-01";
                case "PhoneNumber": //电话号码
                    return "电话格式错误(固话请填写区号,固话10至12位,手机11位)";
                case "Phone": //手机号码
                    return "手机号码格式错误(11位数字)";
                case "ZipCode":    //邮政编码
                    return "邮政编码格式错误(6位数字)";
                case "Money":    //货币（正数或负数）
                    return "金额格式错误(正数或负数)";
                case "IDCard": //身份证号码长度
                    return "身份证号码格式错误(15或18位)";
                default:
                    if (this.ErrorMessage)
                        return this.ErrorMessage;
                    else
                        return "此项填写有误，请认真检查！";
            }
        }

        //失去焦点，获得焦点的时候验证，并显示提示
        var Tboxs = $(":text[EnableCheck=True]");
        var password = $(":password[EnableCheck=True]");
        var Selects = $("select[EnableCheck=True]")
        var CheckBoxList = $("table[EnableCheck=True]");
        var TextAreas = $("textarea[EnableCheck=True]");
        var AreaSelect = $("div[EnableCheck=True]");
        
        Tboxs.focus(CheckWithTip).blur(CheckNoTip).keyup(CheckWithTip).each(function () { this.Check = CheckNoTip; });

        password.focus(CheckWithTip).blur(CheckNoTip).keyup(CheckWithTip).each(function () { this.Check = CheckNoTip; });

        TextAreas.focus(CheckWithTip).blur(CheckNoTip).keyup(CheckWithTip).each(function () { this.Check = CheckNoTip; });

        Selects.focus(CheckWithTip).blur(CheckNoTip).change(CheckWithTip).each(function () { this.Check = CheckNoTip; });

        CheckBoxList.each(function () {
            $(":checkbox", this).click(function () { Check.apply($(this).parent().parent().parent().parent()); })
        });

        AreaSelect.each(function () {
            $("select", this).change(function () { Check.apply(this); })
        });

        $(":submit:not([uncheck='true'])").click(myCheck);
        //myCheck1();

        function myCheck1() {
            var Txts = $(":text[EnableCheck=True]");
            var password = $(":password[EnableCheck=True]");
            var Radios = $(":radio[EnableCheck=True]");
            var Selects = $("select[EnableCheck=True]");
            var CheckBoxList = $("table[EnableCheck=True]");
            var TextAreas = $("textarea[EnableCheck=True]");
            var AreaSelect = $("div[EnableCheck=True]");

            Txts.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).addClass("checkError");
                }

            });
            password.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).addClass("checkError");
                }
            })
            Radios.each(function () {
                if ($(":radio[name='" + this.name + "']:checked").count == 0) {
                    $(this).addClass("checkError");
                }
            })
            Selects.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).addClass("checkError");
                }
            })
            CheckBoxList.each(function () {
                if (!Check.apply(this)) {
                    $(this).addClass("checkError");
                }
            })
            TextAreas.each(function () {
                if (!Check.apply(this)) {
                    $(this).addClass("checkError");
                }
            })
        }

        function myCheck() {
            if (this.checkgroup)
                this.CheckGroup = this.checkgroup;

            if (typeof ($(this).attr("CheckGroup")) != "undefined") {
                var Txts = $(":text[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
                var password = $(":password[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
                var Radios = $(":radio[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']:not(:checked)");
                var Selects = $("select[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");

                var CheckBoxList = $("table[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
                var TextAreas = $("textarea[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
                var AreaSelect = $("div[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
            }
            else {
                var Txts = $(":text[EnableCheck=True]:not([CheckGroup])");
                var password = $(":password[EnableCheck=True][CheckGroup='" + $(this).attr("CheckGroup") + "']");
                var Radios = $(":radio[EnableCheck=True]:not([CheckGroup]:checked)");
                var Selects = $("select[EnableCheck=True]:not([CheckGroup])");
                var CheckBoxList = $("table[EnableCheck=True]:not([CheckGroup])");
                var TextAreas = $("textarea[EnableCheck=True]:not([CheckGroup])");
                var AreaSelect = $("div[EnableCheck=True]:not([CheckGroup])");
            }

            var flag = true;
            Txts.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).showTip();
                    flag = false;
                }
            })
            password.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).showTip();
                    flag = false;
                }
            })
            Radios.each(function () {
                if ($(":radio[name='" + this.name + "']:checked").count == 0) {
                    $(this).showTip();
                    flag = false;
                }
            })
            Selects.each(function () {
                if (!this.Check.apply(this)) {
                    $(this).showTip();
                    flag = false;
                }
            })
            CheckBoxList.each(function () {
                if (!Check.apply(this)) {
                    flag = false;
                    $(this).showTip();
                }
            })
            TextAreas.each(function () {
                if (!Check.apply(this)) {
                    $(this).showTip();
                    flag = false;
                }
            })
            AreaSelect.each(function () {
                if (!Check.apply(this)) {
                    flag = false;
                    $(this).showTip();
                }
            })
            return flag;
        }
    }
})(jQuery);